﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using FisheryPlatform.DataAccess.DBUtility;
using FisheryPlatform.Entity;
using System.Data;

namespace FisheryPlatform.DataAccess.ShareCode
{
    public class ShareCodeClassDB
    {
        #region private const
        private const string PARAM_CLASS_I = "@ClassID";
        private const string PARAM_NAME = "@Name";
        private const string PARAM_NAME_E = "@NameEN";
        private const string PARAM_CODE = "@Code";
        private const string PARAM_CREATE_DATE = "@CreateDate";
        private const string PARAM_LAST_UPDATE_DATE = "@LastUpdateDate";

        private static readonly string ConnectionString = SQLHelper.CONNECTION_STRING;
        #endregion

        private static void FillEntityFrom(SqlDataReader reader, ShareCodeClassEntity obj)
        {
            #region
            if (reader != null && !reader.IsClosed)
            {
                obj.ClassID = reader.IsDBNull(reader.GetOrdinal("ClassID")) ? 0 : reader.GetInt64(reader.GetOrdinal("ClassID"));
                obj.Name = reader.IsDBNull(reader.GetOrdinal("Name")) ? String.Empty : reader.GetString(reader.GetOrdinal("Name"));
                obj.NameEN = reader.IsDBNull(reader.GetOrdinal("NameEN")) ? String.Empty : reader.GetString(reader.GetOrdinal("NameEN"));
                obj.Code = reader.IsDBNull(reader.GetOrdinal("Code")) ? String.Empty : reader.GetString(reader.GetOrdinal("Code"));
                obj.CreateDate = reader.IsDBNull(reader.GetOrdinal("CreateDate")) ? DateTime.MinValue : reader.GetDateTime(reader.GetOrdinal("CreateDate"));
                obj.LastUpdateDate = reader.IsDBNull(reader.GetOrdinal("LastUpdateDate")) ? DateTime.MinValue : reader.GetDateTime(reader.GetOrdinal("LastUpdateDate"));
            }
            #endregion
        }

        private static SqlParameter[] GetShareCodeClassParams(ShareCodeClassEntity obj)
        {
            #region
            SqlParameter[] dbParams ={					
					 SQLHelper.MakeParam(PARAM_CLASS_I, SqlDbType.BigInt,0,obj.ClassID),
					 SQLHelper.MakeParam(PARAM_NAME, SqlDbType.NVarChar, 100,obj.Name.Trim()),					
					 SQLHelper.MakeParam(PARAM_NAME_E, SqlDbType.NVarChar, 200,obj.NameEN),			
					 SQLHelper.MakeParam(PARAM_CODE, SqlDbType.VarChar, 50,obj.Code)				
			};

            return dbParams;
            #endregion
        }

        public static bool Save(ShareCodeClassEntity obj)
        {
            #region
            try
            {
                SqlParameter[] dbParams = GetShareCodeClassParams(obj);

                if (obj.ClassID <= 0)
                {
                    obj.ClassID = Convert.ToInt32(SQLHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "ShareCodeClass_InsertUpdate", dbParams));
                    return obj.ClassID > 0 ? true : false;
                }
                else
                {
                    var affectNum = SQLHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "ShareCodeClass_InsertUpdate", dbParams);
                    return affectNum > 0 ? true : false;
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("ShareCodeClass-->InsertOrUpdate-->" + e.Message);

                ExceptionMessageDB.Record(ex);
            }

            return false;
            #endregion
        }

        public static ShareCodeClassEntity GetByClassId(long classID)
        {
            #region
            SqlDataReader reader = null;
            ShareCodeClassEntity obj = new ShareCodeClassEntity();

            try
            {
                SqlParameter[] dbParams = {
                        SQLHelper.MakeParam(PARAM_CLASS_I, SqlDbType.Int, 0, classID)
                    };

                reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "ShareCodeClass_GetByClassId", dbParams);

                if (reader.Read())
                {
                    FillEntityFrom(reader, obj);
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("ShareCodeClass-->GetByClassId-->" + e.Message);

                ExceptionMessageDB.Record(ex);
            }
            finally
            {
                if (reader != null && !reader.IsClosed)
                    reader.Close();
            }

            return obj;
            #endregion
        }

        public static bool Delete(long classID)
        {
            #region
            try
            {
                SqlParameter[] dbParams = {
                        SQLHelper.MakeParam(PARAM_CLASS_I, SqlDbType.Int, 0, classID)
                    };

                return SQLHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "ShareCodeClass_Delete", dbParams) > 0 ? true : false;
            }
            catch (Exception e)
            {
                Exception ex = new Exception("ShareCodeClass-->Delete-->" + e.Message);

                ExceptionMessageDB.Record(ex);
            }
            return false;
            #endregion
        }

        public static List<ShareCodeClassEntity> Search(string sqlWhere, int pageIndex, int pageSize, out int pageCount)
        {
            #region
            List<ShareCodeClassEntity> result = new List<ShareCodeClassEntity>();
            pageCount = 0;
            try
            {
                if (string.IsNullOrEmpty(sqlWhere))
                {
                    sqlWhere = " 1=1 ";
                }

                SqlParameter[] dbParams =
                {
                    SQLHelper.MakeParam("@sqlWhere", SqlDbType.NVarChar,0,sqlWhere),
                    SQLHelper.MakeParam("@pageIndex",SqlDbType.Int,0,pageIndex),
                    SQLHelper.MakeParam("@pageSize",SqlDbType.Int,0,pageSize),
                    SQLHelper.MakeOutParam("@pageCount", SqlDbType.Int,0,0)                    
                };
                using (SqlDataReader reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "ShareCodeClass_Search", dbParams))
                {
                    while (reader.Read())
                    {
                        ShareCodeClassEntity model = new ShareCodeClassEntity();
                        FillEntityFrom(reader, model);
                        result.Add(model);
                    }
                }
                pageCount = int.Parse(dbParams[3].Value.ToString());
            }
            catch (Exception e)
            {
                Exception ex = new Exception("ShareCodeClass-->Search-->" + e.ToString());
                ExceptionMessageDB.Record(ex);
            }
            return result;
            #endregion
        }
    }
}
